<?php 
namespace Home\Controller;
use Think\Controller;
use Admin\Controller\PublicController;
class KechengController extends CommonController{
	
	function _initialize() {
		parent::_initialize();
		
	}
	
	public function index(){
		//判断是否有班级
		$term = A('Api/Termtime')->get_term_time_now();
		$where = array(
			'uid' => $_SESSION['_loginUser_home']['uid'],
			//'xuenian' => array('like','%X'.$term['id'].'N%')
		);
		$lsinfo = D('LessonStudentRelation')->where($where)->relation(true)->order('create_time desc')->select();
		if($lsinfo){
			foreach ($lsinfo as $key=> $lsif){
				$lsinfo[$key]['orderinfo'] = D('YgclOrder')->where(array('id'=>$lsif['orderid']))->relation(true)->find();
				$lsinfo[$key]['classtype'] = $this->getlessontype($lsif['banji']['lessontype']);
			}
			
		}else{
			$this->redirect('/home/kecheng/none');
			exit();
		}
		$this->lsinfo = $lsinfo;
		$this->display();
	}
	
	//返回课程类型
	public function getlessontype($id){
		return D('YgclClassType')->where(array('id'=>$id))->getField('title');
	}
	
	//课程安排列表
	public function infolist(){
		$user = $_SESSION['_loginUser_home'];
		$role = $user['role'];
		$data = $this->checkrole($role,$user['uid']);
		if(!$data){
			$this->redirect('/home/kecheng/none');
			exit();
		}
		$info = $data[$role];
		foreach ($info as $if){
			$cids[] = $if['cid'];
		}
		$class = D('YgclClass')->where(array('id'=>array('in',$cids)))->relation(true)->select();
		if(count($class) == 1){
			$this->redirect('/home/kecheng/info/cid/'.$class[0]['id']);
		}
		$this->class = $class;
		$this->display();
	}
	
	
	//课程安排
	public function info(){
		$this->display();
	} 
	
	public function getinfo(){
		$cid = I('cid');
		$page = I('page');
		$map['type'] = 'course';
		$map['cid'] = $cid;
		$total = M('YgclWork')->where($map)->count();
		$totalPages = ceil($total/10);//总页数
		$info = D('YgclWork')->where($map)->relation(true)->order('id desc')->page($page,10)->select();
		$totalPages == $page ? $next = 'no':$next = 'yes';
		
		$data = array(
				'status'=>1,
				'info' => $info,
				'next' => $next
		);
		$this->ajaxReturn($data);
	}
	
	//本周要点课程列表
	public function yaodianlist(){
		$user = $_SESSION['_loginUser_home'];
		$role = $user['role'];
		$data = $this->checkrole($role,$user['uid']);
		if(!$data){
			$this->redirect('/home/kecheng/none');
			exit();
		}
		$info = $data[$user['role']];
		foreach ($info as $if){
			$cids[] = $if['cid'];
		}
		$class = D('YgclClass')->where(array('id'=>array('in',$cids)))->relation(true)->select();
		if(count($class) == 1){
			$this->redirect('/home/kecheng/yaodian/cid/'.$class[0]['id']);
		}
		$this->class = $class;
		$this->display();
	}
	
	//本周要点
	public function yaodian(){
		$this->cid = I('cid');
		$info = D('YgclClass')->where(array('id'=>$this->cid))->relation(true)->find();
		$this->classname= $info['classname'];
		$this->display();
	}
	
	public function getyaodian(){
		$cid = I('cid');		
		$page = I('page',1);
		$data = $this->getweek($cid, $page,10);
// 		$info['content'] = str_to_html($info['content']);
		$this->ajaxReturn($data);
	}
	
	//作业布置
	public function zuoye(){
		$lid = I('lid');
		$lessoninfo = D('YgclLesson')->where(array('id'=>$lid))->relation(true)->find();
		$this->lessonname= $lessoninfo['lessonname'];
		$this->display();
	}
	
	public function getzuoye(){
		$lid = I('lid');
		$page = I('page',1);
		$data = $this->getweek('weekwork', $lid, $page);
		$this->ajaxReturn($data);
	}
	
	public function getweek($cid,$page,$pagesize){
		$type_config = array(
			'weekkey' => '每周课程内容',
			'weekquwei' => '课堂趣味',
			'weekwork' => '布置作业'
		);
		$map = array(
			'type' => array('in',array('weekkey','weekquwei','weekwork')),
			'cid' => $cid,
			'status' => 1
 		);
		$total = M('YgclWork')->where($map)->count();
		$totalPages = ceil($total/$pagesize);//总页数
		
		$info = D('YgclWork')->where($map)->relation(true)->order('id desc')->page($page,$pagesize)->select();
		$totalPages == $page ? $next = 'no':$next = 'yes';
		
		$m = D('Users');
		
		
// 		if($type == 'weekwork'){
// 			$m_work = D('UsersDistrictRelation');
// 			$cid = D('YgclLesson')->where(array('id'=>$lid))->getField('cid');
// 			$t = $m_work->where(array('cid'=>$cid))->getField('uid');
// 			$teacher_weekwork = $m->where(array('uid'=>$t))->getField('realname');
// 		}
		
		
		
		foreach ($info as $key=>$if){
			$info[$key]['type_title'] = $type_config[$if['type']];
// 			if($type == 'weekkey'){
// 				$user = $m->where(array('uid'=>$if['uid']))->find();
// 				if($user['role'] == 'teacher'){
// 					$teacher = splitName($user['realname'])[0].'老师';
// 				}
// 			}
// 			if($type == 'weekwork'){
// 				$teacher = splitName($teacher_weekwork)[0].'老师';
// 			}
// 			$info[$key]['teacher'] = $teacher;
		}
		$data = array(
				'next'=>$next,
				'info'=>$info,
		);
		return $data;
	}
	
	//没有课程
	public function none(){
		$this->display();
	}
	
	public function article(){
		$id = I('id');
		$this->info = D('YgclWork')->where(array('id'=>$id))->relation(true)->find();
		$pingjia = D('YgclPingjia')->where(array('wid'=>$id,'uid'=>$_SESSION['_loginUser_home']['uid']))->relation(true)->order('id desc')->select();
// 		dump($pingjia);
		if($pingjia){
			$this->t = 1;
		}else{
			$this->t = 0;
		}
		$this->pingjia = $pingjia;
		$this->display();
	}
	
	public function checkrole($role,$uid){
		$xn_now = R('Api/Termtime/get_term_time_now');
		$xnid = $xn_now['id'];
		$bool = false;
		if($role === 'user'){
			$map = array(
				'uid'=>$uid,
				'xuenian'=> array('like','%X'.$xnid.'N%')
			);
			$info = D('LessonStudentRelation')->where($map)->relation(true)->select();
			if($info){
				$bool = true;
			}
		}
		if($role === 'teacher'){
			$info = D('UsersDistrictRelation')->where(array('uid'=>$uid))->select();
			if($info){
				$bool = true;
			}
		}
		if(!$bool){
			return $bool;
		}else{
			return array($role=>$info);
		}
	}
	
	//教师评价-课程列表
	public function teacherpjlist(){
		$user = $_SESSION['_loginUser_home'];
		$role = $user['role'];
		if($role === 'teacher'){
			$this->redirect('/home/kecheng/none');
			exit();
		}
		$data = $this->checkrole($role,$user['uid']);
		if(!$data){
			$this->redirect('/home/kecheng/none');
			exit();
		}
		$info = $data[$user['role']];
		foreach ($info as $if){
			$cids[] = $if['cid'];
		}
		$class = D('YgclClass')->where(array('id'=>array('in',$cids)))->relation(true)->select();
		if(count($class) == 1){
			$this->redirect('/home/kecheng/teacherpj/cid/'.$class[0]['id']);
		}
		$this->class = $class;
		$this->display();
	}
	
	public function teacherpj(){
		$cid = I('cid');
		$class = D('YgclClass')->where(array('id'=>$cid))->relation(true)->find();
		
		$uid = M('UsersDistrictRelation')->where(array('cid'=>$cid))->getField('uid');
		$user = D('Users')->where(array('uid'=>$uid))->relation(true)->find();

		$pingjia = D('YgclPingjiaTeacher')->where(array('uid'=>$_SESSION['_loginUser_home']['uid']))->relation(true)->order('id desc')->select();
		$html = 
		$this->info = '课程：'.$class['title'].'<br/>教师：'.$user['realname'];
		$this->cid = $cid;
		$this->teacherid = $uid;
		$this->pingjia = $pingjia;
		$this->display();
	}
	
	public function teacherpj_post(){
		
	}
}
?>